import javax.swing.plaf.nimbus.State;
import java.sql.*;

public class Main {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            //1.创建数据库链接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "1234567899");

            //2.获取数据库操作对象,操作sql语句,Statement 对象是专门执行sql语句的
            statement  = connection.createStatement();

            // 执行sql语句
            String sql  = "insert into dept(deptno,dname,loc) values (29,'人事部','北京')";
            //executeUpdate 是专门执行insert delete update 语句的,返回值是"数据库中被影响的记录的条数"
            int ret = statement.executeUpdate(sql);
            System.out.println(ret);

            //4.处理查询结果即




            // 进行数据库操作
            System.out.println(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            //5.为了保证资源一定释放,在finally语句中释放资源,遵循从小到大依次关闭
            // 分别对其进行try catch进行关闭
            try{
                if(statement!=null)
                {
                    statement.close();
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }


            try{
                if(connection!=null)
                {
                    connection.close();
                }
            }
            catch (SQLException e){
                e.printStackTrace();
            }

        }
    }
}

